/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.adama.service;

import fr.adama.dto.EmployeDTO;
import fr.adama.entite.Employe;
import fr.adama.repository.EmployeRepository;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author avisto
 */
@Service
@Transactional
public class EmployeService extends CrudService {

    @Autowired
    EmployeRepository employeRepository;

    public <T> List<T> findByNom(Class entity, Class entityDTO, String nom) {

        List list = crudRepository.findByCriteria(entity, Restrictions.like("nom", nom, MatchMode.START));
        List result = new ArrayList<>();
        for (Object objet : list) {
            Object dozerResult = mapperDozer.map(objet, entityDTO);
            result.add(dozerResult);
        }
        return result;

    }

    public List<EmployeDTO> searchByEntity(EmployeDTO employe) {
        List<EmployeDTO> list = new ArrayList<>();

        List result = employeRepository.searchByField(employe);
        for (Object objet : result) {
            EmployeDTO dozerResult = mapperDozer.map(objet, employe.getClass());
            list.add(dozerResult);
        }

        return list;
    }

   

    public EmployeDTO findByNomPrenom(String nom, String prenom) {
        Employe list;

        list = employeRepository.getByNomPrenom(nom, prenom);
        if (list == null) {
            return null;
        }
        EmployeDTO dozerResult = mapperDozer.map(list, EmployeDTO.class);

        return dozerResult;
    }

}
